﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;
using System.Collections.Specialized;
using System.Data.SqlClient;
using System.Data;
using System.Web;

namespace POSSystem.BLL
{
    public class Customers : _Customers
    {
        public Customers()
        {
            this.AddColumn("TypeName", typeof(string));
        }

        public virtual void UpdateCustomerAmt(int custid,
                decimal totalAmt,
                decimal paidAmt)
        {
            ListDictionary parameters = new ListDictionary();

            parameters.Add(new SqlParameter("@custid", SqlDbType.Int, 0), custid);
            parameters.Add(new SqlParameter("@totalAmt", SqlDbType.Decimal, 0), totalAmt);
            parameters.Add(new SqlParameter("@paidAmt", SqlDbType.Decimal, 0), paidAmt);

            LoadFromSqlNoExec("updateCustomerAmt", parameters);

        }

        public static Customers GetAllActiveCustomers()
        {
            try
            {
                Customers customersTb = new Customers();
                customersTb.Where.Deleted.Value = false;
                customersTb.Where.BranchID.Value = Branches.CurrentBranchID;
                customersTb.Query.Load();

                return customersTb;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Customers.cs", "GetAllActiveCustomers", ex.StackTrace);
                throw new Exception("Customers.GetAllActiveCustomers(): " + ex.Message);
            }
        }

        public string TypeName
        {
            get { return this.GetColumn("TypeName").ToString(); }
            set { this.SetColumn("TypeName", value); }
        }

        public void AddNewCustomer(string customerName, string company, string address, string phone, string fax,
            string mobile, string email, string password, int customerType)
        {
            try
            {
                this.AddNew();
                //fields
                this.Deleted = false;
                this.CustomerName = customerName;
                this.Company = company;
                this.Address = address;
                this.Phone = phone;
                this.Fax = fax;
                this.Mobile = mobile;
                this.Email = email;
                this.Password = password;
                this.CustomerType = customerType;
                this.LastModifiedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);
                this.BranchID = Branches.CurrentBranchID;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Customer.cs", "AddNewCustomer", ex.StackTrace);
                throw new Exception("Customers.AddNewCustomer(): " + ex.Message);
            }
        }

        public void UpdateCustomerInfo(int customerID, string customerName, string company, string address, string phone, string fax,
            string mobile, string email, string password, int customerType)
        {
            //throw new NotImplementedException();
            try
            {
                //this.Where.ID.Value = customerID;
                this.Filter = Customers.ColumnNames.ID + " = " + customerID.ToString();
                this.CustomerName = customerName;
                this.Company = company;
                this.Address = address;
                this.Phone = phone;
                this.Fax = fax;
                this.Mobile = mobile;
                this.Email = email;
                this.Password = password;
                this.CustomerType = customerType;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("Customer.cs", "UpdateCustomerInfo", ex.StackTrace);
                throw new Exception("Customers.UpdateCustomerInfo(): " + ex.Message);
            }
        }

        //public void DeleteCustomerInfo(int id)
        //{
        //    this.Where.ID.Value = id;
        //    this.MarkAsDeleted();
        //}

        public void SetAllDeleted() //set filed Deleted of all rows = true
        {
            if (RowCount > 0)
            {
                Rewind(); //move to first record 
                do
                {
                    //do something with the current row 
                    this.Deleted = true;
                } while (this.MoveNext());
                this.Save();
            }
        }

        public static void ChangePassword(int id, string pass)
        {
            Customers c = new Customers();
            c.LoadByPrimaryKey(id);

            if (c.RowCount > 0)
            {
                c.Password = pass;
                c.Save();
            }
        }

        public static Customers GetCustomerByEmail(string email)
        {
            Customers c = new Customers();
            c.Where.Email.Value = email;
            c.Where.BranchID.Value = Branches.CurrentBranchID;
            c.Where.Deleted.Value = false;
            c.Query.Load();

            return c;
        }

        public static Customers GetCustomerByEmail(string email, int custID)
        {
            Customers c = new Customers();
            c.Where.Email.Value = email;
            c.Where.BranchID.Value = Branches.CurrentBranchID;
            c.Where.Deleted.Value = false;
            c.Where.ID.Value = custID;
            c.Where.ID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.NotEqual;
            c.Query.Load();

            return c;
        }
    }
}
